package com.ecoolex.hotel.saas.api.graphql;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import com.alibaba.fastjson.JSON;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMemberAuthority;
import com.ecoolex.framework.modularize.api.service.storage.StorageInfo;
import com.ecoolex.framework.modularize.api.service.storage.StorageService;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;
import com.ecoolex.hotel.saas.entity.PresaleCouponOrder;
import com.ecoolex.hotel.saas.entity.PresaleCouponOrderAfterSales;
import com.ecoolex.hotel.saas.entity.PresaleCouponOrderAfterSalesRecord;
import com.ecoolex.hotel.saas.enums.PresaleCouponOrderAfterSalesStatus;
import com.ecoolex.hotel.saas.ws.PresaleCouponOrderService;
import com.google.common.collect.Lists;

@GraphQL2Module(type = Type.User, description = "预售券售后单")
public class PresaleCouponOrderAfterSalesGraphql {

	@Reference
	private PresaleCouponOrderService presaleCouponOrderService;
	
	@Autowired
	private StorageService storageService;
	
	@TypeField(type = PresaleCouponOrderAfterSales.class, field = "canDelete", description = "能删除售后单")
	public Boolean canDelete(@Source PresaleCouponOrderAfterSales source) {
		return PresaleCouponOrderAfterSalesStatus.canDelete(source.getStatus());
	}
	
	@TypeField(type = PresaleCouponOrderAfterSales.class, field = "canCancel", description = "能取消售后单申请")
	public Boolean canCancel(@Source PresaleCouponOrderAfterSales source) {
		return PresaleCouponOrderAfterSalesStatus.canDealWith(source.getStatus());
	}
	
	@TypeField(type = PresaleCouponOrderAfterSales.class, field = "presaleCouponOrderAfterSalesRecordList", description = "预售券售后记录")
	public List<PresaleCouponOrderAfterSalesRecord> afterSalesRecord(@Source PresaleCouponOrderAfterSales source){
		return presaleCouponOrderService.afterSalesRecord(source.getAfterSalesId());
	}
	
	@TypeField(type = PresaleCouponOrderAfterSales.class, field = "presaleCouponOrder", description = "预售券订单")
	public PresaleCouponOrder presaleCouponOrder(@Source PresaleCouponOrderAfterSales source) {
		return presaleCouponOrderService.get(source.getOrderId());
	}
	
	@TypeField(type = PresaleCouponOrderAfterSales.class, field = "evidenceImage", description = { "凭证图片" })
	public List<StorageInfo> evidenceImage(@Source PresaleCouponOrderAfterSales source) {
		if(Check.notNullOrEmpty(source.getEvidenceImage())) {
			return JSON.parseArray(source.getEvidenceImage(), String.class).stream()
				.map(storageService::get)
				.filter(Check::notNull)
				.collect(Collectors.toList());
		}
		return Lists.newArrayList();
	}
	
	@QueryField(field = "presaleCouponOrderAfterSales", description = "预售券售后单详情")
	public PresaleCouponOrderAfterSales get(@NonNull Integer afterSalesId, ConsumerMemberAuthority user) {
		user.assertLogin();
		return presaleCouponOrderService.getAfterSales(afterSalesId);
	}
	
	@QueryField(field = "presaleCouponOrderAfterSalesList", description = "预售券售后单列表")
	public Pagination<PresaleCouponOrderAfterSales> query(@NonNull Integer storeId, @NonNull Page page, ConsumerMemberAuthority user){
		user.assertLogin();
		return presaleCouponOrderService.queryAfterSalesForUser(storeId, user.userId(), page);
	}
	
	@MutationField(field = "cancelPresaleCouponOrderAfterSalesApply", description = "取消售后申请")
	public PresaleCouponOrderAfterSales cancelApply(@NonNull Integer afterSalesId, ConsumerMemberAuthority user) {
		user.assertLogin();
		PresaleCouponOrderAfterSales afterSales = presaleCouponOrderService.getAfterSales(afterSalesId);
		Assert.isTrue(Check.notNull(afterSales),ResultCode.VERIFY.build(1), "售后单不存在");
		return presaleCouponOrderService.cancelApply(afterSales, user.userName());
	}
	
	@MutationField(field = "deletePresaleCouponOrderAfterSales", description = "删除售后单")
	public PresaleCouponOrderAfterSales delete(@NonNull Integer afterSalesId, ConsumerMemberAuthority user) {
		user.assertLogin();
		PresaleCouponOrderAfterSales afterSales = presaleCouponOrderService.getAfterSales(afterSalesId);
		Assert.isTrue(Check.notNull(afterSales),ResultCode.VERIFY.build(1), "售后单不存在");
		return presaleCouponOrderService.delete(afterSales, user.userName());
	}
}
